package com.meixiang.adapter.shopping;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.shopping.GoodsGroupDetailEntity;
import com.meixiang.entity.shopping.GoodsSpecEntity;
import com.meixiang.tool.Tool;
import com.meixiang.util.GlideHelper;
import com.meixiang.util.TextViewUtil;
import com.meixiang.view.ImageGallery;
import com.meixiang.view.LineView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 优惠套装列表
 *
 * @author LZRUI
 */
public class PrivilegeSuitListExpandableAdapter extends BaseExpandableListAdapter {

    /**
     * 建立枚举 2个item 类型
     */
    enum ITEM_TYPE {
        NORMAL,
        TO_SHOPPING_CAR
    }

    private Activity mActivity;
    private List<GoodsGroupDetailEntity> groupList;
    private GroupGoodsClickListener listener;

    public void setGroupGoodsClickListener(GroupGoodsClickListener listener) {
        this.listener = listener;
    }

    public void refreshData(List<GoodsGroupDetailEntity> list) {
        this.groupList = list;
        notifyDataSetChanged();
    }

    public PrivilegeSuitListExpandableAdapter(Activity activity) {
        this.mActivity = activity;
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Tool.isFastDoubleClick()) return;
            if (listener == null) return;
            int gp = (Integer) v.getTag(R.id.group_position);
            int cp = (Integer) v.getTag(R.id.child_position);
            switch (v.getId()) {
                // 商品item
                case R.id.rl_suit:
                    // TODO 进入商品列表
                    listener.showDetail(gp, cp);
                    break;
                // 加入购物车
                case R.id.tv_add_shopping_car:
                    listener.addToCart(gp, cp);
                    break;
            }
        }
    };

    @Override
    public int getGroupCount() {
        return groupList == null ? 0 : groupList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return groupList == null || groupList.get(groupPosition) == null || groupList.get(groupPosition).getGoodsSpecList() == null ? 0 : groupList.get(groupPosition).getGoodsSpecList().size() + 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groupList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return groupList.get(groupPosition).getGoodsSpecList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @SuppressWarnings("unchecked")
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_suit_group, parent, false);
            viewHolder = new GroupViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (GroupViewHolder) convertView.getTag();
        }
        // TODO 设置数据
        GoodsGroupDetailEntity groupEntity = groupList.get(groupPosition);
        if (groupEntity != null) {
            viewHolder.tvSuitName.setText(groupEntity.getGoodsName());
            viewHolder.tvSaveMoney.setText(String.format(mActivity.getString(R.string.save_money), groupEntity.getBenefit()));
            List<String> urlList = new ArrayList<>();
            List<GoodsSpecEntity> childList = groupEntity.getGoodsSpecList();
            int len = childList.size();
            for (int i = 0; i < len; i++) {
                urlList.add(childList.get(i).getGoodsImage());
            }
            viewHolder.imageGallery.setFocusable(false);
            viewHolder.imageGallery.setClickable(false);
            viewHolder.imageGallery.setMargin(0, 10, 10, 10);
            viewHolder.imageGallery.setData(urlList, false);
            if (isExpanded) {
                // 打开状态
                viewHolder.imageGallery.setVisibility(View.GONE);
                viewHolder.ivIndicator.setEnabled(true);
            } else {
                viewHolder.imageGallery.setVisibility(View.VISIBLE);
                viewHolder.ivIndicator.setEnabled(false);
            }
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        GoodsGroupDetailEntity groupEntity = groupList.get(groupPosition);
        if (groupEntity != null) {
            if (isLastChild) {
                AddShoppingCarViewHolder viewHolder;
                if (convertView == null) {
                    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_suit_child_add_shopping_car, parent, false);
                    viewHolder = new AddShoppingCarViewHolder(convertView);
                    convertView.setTag(viewHolder);
                } else {
                    viewHolder = (AddShoppingCarViewHolder) convertView.getTag();
                }
                // TODO 设置数据
                viewHolder.tvPrivilegePrice.setText(String.format(mActivity.getString(R.string.privilege_price), groupEntity.getGoodsStorePrice()));
                TextViewUtil.setStrickoutText(viewHolder.tvOldPrice, String.format(mActivity.getString(R.string.price_str), groupEntity.getGoodsMarketPrice()));

                // 设置监听
                viewHolder.tvAddShoppingCar.setTag(R.id.group_position, groupPosition);
                viewHolder.tvAddShoppingCar.setTag(R.id.child_position, childPosition);
                viewHolder.tvAddShoppingCar.setOnClickListener(onClickListener);

            } else {
                ChildViewHolder viewHolder;
                if (convertView == null) {
                    convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_suit_child, parent, false);
                    viewHolder = new ChildViewHolder(convertView);
                    convertView.setTag(viewHolder);
                } else {
                    viewHolder = (ChildViewHolder) convertView.getTag();
                }
                // TODO 设置数据
                GoodsSpecEntity childEntity = groupEntity.getGoodsSpecList().get(childPosition);
                GlideHelper.showImage(mActivity, childEntity.getGoodsImage(), viewHolder.ivGoodsLogo);
                viewHolder.tvGoodsName.setText(childEntity.getGoodsName());
                if (childPosition == 1) {
                    viewHolder.lineView.setMarginLeft(0);
                } else {
                    viewHolder.lineView.setMarginLeft(15);
                }
                // 设置监听
                viewHolder.rlSuit.setTag(R.id.group_position, groupPosition);
                viewHolder.rlSuit.setTag(R.id.child_position, childPosition);
                viewHolder.rlSuit.setOnClickListener(onClickListener);

            }
        }
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public int getChildType(int groupPosition, int childPosition) {
        // 如果是最后一个childPosition， 就显示加入购物车的item
        return childPosition == groupList.get(groupPosition).getGoodsSpecList().size() ? ITEM_TYPE.TO_SHOPPING_CAR.ordinal() : ITEM_TYPE.NORMAL.ordinal();
    }

    @Override
    public int getChildTypeCount() {
        return 2;
    }

    static class GroupViewHolder {
        @Bind(R.id.tv_suit_name)
        TextView tvSuitName;
        @Bind(R.id.tv_save_money)
        TextView tvSaveMoney;
        @Bind(R.id.iv_indicator)
        ImageView ivIndicator;
        @Bind(R.id.img_gallery)
        ImageGallery imageGallery;

        public GroupViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ChildViewHolder {
        @Bind(R.id.iv_goods_logo)
        ImageView ivGoodsLogo;
        @Bind(R.id.tv_goods_name)
        TextView tvGoodsName;
        @Bind(R.id.line_view)
        LineView lineView;
        @Bind(R.id.rl_suit)
        RelativeLayout rlSuit;

        public ChildViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class AddShoppingCarViewHolder {
        @Bind(R.id.tv_privilege_price)
        TextView tvPrivilegePrice;
        @Bind(R.id.tv_old_price)
        TextView tvOldPrice;
        @Bind(R.id.tv_add_shopping_car)
        TextView tvAddShoppingCar;

        public AddShoppingCarViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    /**
     * 点击回调
     */
    public interface GroupGoodsClickListener {
        void addToCart(int groupPosition, int childPosition);

        void showDetail(int groupPosition, int childPosition);
    }

}
